package cn.echo.modules.sys.service;

import cn.echo.modules.sys.dao.AreaMapper;
import cn.echo.modules.sys.entity.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 王飞
 * @Date 2017-4-13
 * @version 1.0
 * 该类是对数据库的具体操作，包括：
 * 1.建立数据库工厂，并与数据库建立连接
 * 2.调用dao层AreaMapper接口里面对应的操作数据库的方法
 * 3.提交事件，并且关闭工厂（关闭连接）
 * 4.将需要的返回值提交给controller层
 * 5.将更改后的数据通过controller层提交给页面进行显示
 */
@Service
public class AreaService {
    @Autowired
    private AreaMapper areaMapper;
    public List<Area> findAll(Area area) {
        //SqlSession session = null;
        //session = MyBatisUtils.getSqlFactory().openSession();
        //AreaMapper areaMapper = session.getMapper(AreaMapper.class);
        List<Area> areaList = areaMapper.findAll(new Area());
        //session.close();
        return areaList;
    }
    public boolean delete(String id)
    {
        //SqlSession session = null;
        //session = MyBatisUtils.getSqlFactory().openSession(true);
        //AreaMapper areaMapper = session.getMapper(AreaMapper.class);
        Area area=new Area();
        area.setId(id);
        boolean result = areaMapper.deleteById(area);
        //session.commit();
        //session.close();
        return result;
    }
    public boolean update(String id,String province,String city,String area)
    {
        //SqlSession session = null;
        //session = MyBatisUtils.getSqlFactory().openSession(true);
        //AreaMapper areaMapper = session.getMapper(AreaMapper.class);
        Area areaParamer=new Area();
        areaParamer.setId(id);
        areaParamer.setName(province);
        areaParamer.setParentId(city);
        areaParamer.setCode(area);
        boolean result=areaMapper.updateById(areaParamer);
        //session.commit();
        //session.close();
        return result;
    }
}
